<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改公寓信息')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-apartmentInfo-edit" th:object="${apartmentInfo}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在省份：</label>
                <div class="col-sm-8">
                    <select id="province" name="addressProvince" class="form-control" required>
                        <option value="">请选择省份</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在城市：</label>
                <div class="col-sm-8">
                    <select id="city" name="addressCity" class="form-control" required>
                        <option value="">请选择城市</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在区/县：</label>
                <div class="col-sm-8">
                    <select id="neighborhood" name="addressNeighborhood" class="form-control" required>
                        <option value="">请选择区/县</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓单元：</label>
                <div class="col-sm-8">
                    <input name="unit" th:field="*{unit}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓门牌号：</label>
                <div class="col-sm-8">
                    <input name="doorNumber" th:field="*{doorNumber}" class="form-control" type="number" min="0101" max="9950" required
                    pattern="^(?:0[1-9]|[1-9]\d)(?:0[1-9]|[1-4]\d|50)$"
                    oninvalid="this.setCustomValidity('请输入正确的门牌号')"
                    oninput="this.setCustomValidity('')">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓户型：</label>
                <div class="col-sm-8">
                    <select name="apartmentType" class="form-control" th:with="type=${@dict.getType('apartment_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{apartmentType}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">租金￥：</label>
                <div class="col-sm-8">
                    <input name="rent" th:field="*{rent}" class="form-control" type="text" min="0.01" step="0.01" required
                    oninvalid="this.setCustomValidity('请输入大于0的有效金额')"
                    oninput="this.setCustomValidity('')">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">公寓状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('apartment_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "apartment/apartmentInfo";
    $("#form-apartmentInfo-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-apartmentInfo-edit').serialize());
        }
    }

    $(function() {
        const apiKey = '3f6aae6f9d881024254e76652abbb1c4'; // 替换为您的高德 API 密钥
        const currentProvince = /*[[${apartmentInfo.addressProvince}]]*/ '';
        const currentCity = /*[[${apartmentInfo.addressCity}]]*/ '';
        const currentNeighborhood = /*[[${apartmentInfo.addressNeighborhood}]]*/ '';

        // 获取省份并加载默认数据
        function loadProvinces() {
            return $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=1&extensions=base`).then(function(data) {
                if (data.status === '1') {
                    data.districts[0].districts.forEach(function(province) {
                        $('#province').append(`<option value="${province.name}" ${province.name === currentProvince ? 'selected' : ''}>${province.name}</option>`);
                    });
                    return $('#province').val();
                } else {
                    console.error('API返回省份数据失败', data.info);
                }
            });
        }

        // 获取城市并加载默认数据
        function loadCities(provinceName) {
            return $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=2&extensions=all&keywords=${provinceName}`).then(function(data) {
                if (data.status === '1') {
                    $('#city').empty().append('<option value="">请选择城市</option>');
                    data.districts[0].districts.forEach(function(city) {
                        $('#city').append(`<option value="${city.name}" ${city.name === currentCity ? 'selected' : ''}>${city.name}</option>`);
                    });
                    return $('#city').val();
                } else {
                    console.error('API返回城市数据失败', data.info);
                }
            });
        }

        // 获取区县并加载默认数据
        function loadDistricts(cityName) {
            return $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=3&extensions=all&keywords=${cityName}`).then(function(data) {
                if (data.status === '1') {
                    $('#neighborhood').empty().append('<option value="">请选择区/县</option>');
                    data.districts[0].districts.forEach(function(district) {
                        $('#neighborhood').append(`<option value="${district.name}" ${district.name === currentNeighborhood ? 'selected' : ''}>${district.name}</option>`);
                    });
                } else {
                    console.error('API返回区县数据失败', data.info);
                }
            });
        }

        // 初始化加载
        loadProvinces().then(function(provinceName) {
            if (provinceName) {
                return loadCities(provinceName);
            }
        }).then(function(cityName) {
            if (cityName) {
                return loadDistricts(cityName);
            }
        });

        // 省份选择变化时获取城市
        $('#province').change(function() {
            const provinceName = $(this).val();
            $('#city').empty().append('<option value="">请选择城市</option>');
            $('#neighborhood').empty().append('<option value="">请选择区/县</option>');

            if (provinceName) {
                loadCities(provinceName).then(function(cityName) {
                    if (cityName) {
                        loadDistricts(cityName);
                    }
                });
            }
        });

        // 城市选择变化时获取区县
        $('#city').change(function() {
            const cityName = $(this).val();
            $('#neighborhood').empty().append('<option value="">请选择区/县</option>');

            if (cityName) {
                loadDistricts(cityName);
            }
        });
    });
</script>
</body>
</html>